package br.com.werp.suporte.Usuario;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import com.egen.util.jdbc.*;
import com.egen.util.text.*;

/**
 * Creation Date 06/09/2004 14:10:45
 * Last Modify Date 21/12/2005 13:41:40
 */

public class DetalheGruposAction extends com.egen.util.struts.AbstractAction {

  public ActionForward perform_update_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    if (com.egen.util.system.Security.isUserInRole(request, "SUP_ADM",false)) {
      HttpSession session = request.getSession(true);
      ActionForward actionForward = null;
      com.egen.util.jdbc.JdbcUtil j = null;
      try {
        DetalheGruposActionForm f = (DetalheGruposActionForm) form;
        j = new com.egen.util.jdbc.JdbcUtil();
        update_action(f, j, request);
        actionForward = mapping.findForward("voltar");
      } catch (Exception e) {
        ActionErrors errors = new ActionErrors();
        errors.add("ActionErrors.GLOBAL_ERROR",
            new ActionError("error.action.exception",
                (e)));
        request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
        session.setAttribute("exception",
            (e));
        actionForward = mapping.findForward("same");
      }
      finally { if (j != null) {
            j.close();
          }
              } return actionForward;
    } else {
      ActionErrors errors_role = new ActionErrors();
      errors_role.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.actiondenied",""));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors_role);
      return mapping.findForward("same");
    }
  }
  private void update_action(DetalheGruposActionForm f,
      com.egen.util.jdbc.JdbcUtil j,
      HttpServletRequest request) throws Exception {
    HttpSession session = request.getSession(true);
    br.com.werp.suporte.dbobj.table.Grupos table =
      new br.com.werp.suporte.dbobj.table.Grupos();
    Vector sets = new Vector(10, 2);
    table.setGrupos_id(
      com.egen.util.text.FormatNumber.parseInt(f.getGrupos_id()));
    table.setGrupos_nome(f.getGrupos_nome());
    if (f.getGrupos_nome() != null) {
      sets.addElement("grupos_nome");
    } else {
      sets.addElement("");
    }
    String[] set = new String[sets.size()];
    for (int i = 0; i < sets.size(); i++) {
      set[i] = (String) sets.elementAt(i);
    }
    int updated = j.update(table, set);
    int pos = 0;
    try {
      pos = (new Integer(f.getPos())).intValue();
    } catch (Exception e) {}
    if (updated == 0) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.norowsupdated",": " + updated));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
    } else {
      Vector res_Grupos = (Vector) session.getAttribute("res_Grupos");
      if (res_Grupos != null) {
        res_Grupos.setElementAt(table, pos);
        br.com.werp.suporte.dbobj.table.Grupos r =
          (br.com.werp.suporte.dbobj.table.Grupos)
          res_Grupos.elementAt(pos);
        f.setGrupos_id(r.getGrupos_id() + "");
        f.setGrupos_nome((r.getGrupos_nome() != null) ?
            r.getGrupos_nome() + "":"");
        f.setPos("" + pos);
      }
    }
    session.setAttribute("DetalheGruposActionForm",f);
  }

  public ActionForward perform_delete_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    if (com.egen.util.system.Security.isUserInRole(request, "SUP_ADM",false)) {
      HttpSession session = request.getSession(true);
      ActionForward actionForward = null;
      com.egen.util.jdbc.JdbcUtil j = null;
      try {
        DetalheGruposActionForm f = (DetalheGruposActionForm) form;
        j = new com.egen.util.jdbc.JdbcUtil();
        delete_action(f, j, request);
        actionForward = mapping.findForward("voltar");
      } catch (Exception e) {
        ActionErrors errors = new ActionErrors();
        errors.add("ActionErrors.GLOBAL_ERROR",
            new ActionError("error.action.exception",
                (e)));
        request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
        session.setAttribute("exception",
            (e));
        actionForward = mapping.findForward("same");
      }
      finally { if (j != null) {
            j.close();
          }
              } return actionForward;
    } else {
      ActionErrors errors_role = new ActionErrors();
      errors_role.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.actiondenied",""));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors_role);
      return mapping.findForward("same");
    }
  }
  private void delete_action(DetalheGruposActionForm f,
      com.egen.util.jdbc.JdbcUtil j,
      HttpServletRequest request) throws Exception {
    HttpSession session = request.getSession(true);
    br.com.werp.suporte.dbobj.table.Grupos table =
      new br.com.werp.suporte.dbobj.table.Grupos();
    table.setGrupos_id(
      com.egen.util.text.FormatNumber.parseInt(f.getGrupos_id()));
    table.setGrupos_nome(f.getGrupos_nome());
    j.delete(table);
    int pos = 0;
    try {
      pos = (new Integer(f.getPos())).intValue();
    } catch (Exception e) {}
    if (pos > 0) {
      Vector res_Grupos = (Vector) session.getAttribute("res_Grupos");
      if (res_Grupos != null) {
        res_Grupos.removeElementAt(pos);
        pos--;
        br.com.werp.suporte.dbobj.table.Grupos r =
          (br.com.werp.suporte.dbobj.table.Grupos)
          res_Grupos.elementAt(pos);
        f.setGrupos_id(r.getGrupos_id() + "");
        f.setGrupos_nome((r.getGrupos_nome() != null) ?
            r.getGrupos_nome() + "":"");
        f.setPos("" + pos);
      }
    } else {
      f = new DetalheGruposActionForm();
    }
    session.setAttribute("DetalheGruposActionForm",f);
  }

  public ActionForward perform_back_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      DetalheGruposActionForm f = (DetalheGruposActionForm) form;
      j = new com.egen.util.jdbc.JdbcUtil();


      actionForward = mapping.findForward("voltar");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }

  public ActionForward perform_pos_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    DetalheGruposActionForm f = (DetalheGruposActionForm) form;
    try {
      pos_action(f, request);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    return actionForward;
  }
  private void pos_action(DetalheGruposActionForm f,
      HttpServletRequest request) throws Exception {
    HttpSession session = request.getSession(true);
    int pos = 0;
    try {
      if (f.getPos() != null && f.getPos().length() > 0) {
        pos = (new Integer(f.getPos())).intValue();
      } else {
        pos = (new Integer((String) session.getAttribute("pos0"))).
            intValue();
      }
    } catch (Exception e) {}
    Vector res_Grupos = (Vector) session.getAttribute("res_Grupos");
    if (res_Grupos != null && pos < res_Grupos.size()) {
      br.com.werp.suporte.dbobj.table.Grupos r =
        (br.com.werp.suporte.dbobj.table.Grupos)
        res_Grupos.elementAt(pos);
      f.setGrupos_id(r.getGrupos_id() + "");
      f.setGrupos_nome((r.getGrupos_nome() != null) ?
          r.getGrupos_nome() + "":"");
      f.setPos("" + pos);
    }
    session.setAttribute("pos0", "" + pos);
    session.setAttribute("DetalheGruposActionForm",f);
  }

  public ActionForward perform_select_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      DetalheGruposActionForm f = (DetalheGruposActionForm) form;
      j = new com.egen.util.jdbc.JdbcUtil();
      select_action(f, j, request);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void select_action(DetalheGruposActionForm f,
      com.egen.util.jdbc.JdbcUtil j,
      HttpServletRequest request) throws Exception {
    HttpSession session = request.getSession(true);
    br.com.werp.suporte.dbobj.table.Grupos table =
      new br.com.werp.suporte.dbobj.table.Grupos();
    String[][] select = null;
    String[] order = null;
    String[] groupby = null;
    String[] having = null;
    Object[][] where = { {"Grupos_id","=",
        com.egen.util.text.FormatNumber.toInt(f.getGrupos_id())}, {"Grupos_nome",
            "like", f.getGrupos_nome()}, };
    Vector res_Grupos = j.select(table, select, where, groupby, having, order);
    session.setAttribute("res_Grupos",res_Grupos);
    if (res_Grupos != null && res_Grupos.size() > 0) {
      br.com.werp.suporte.dbobj.table.Grupos r =
        (br.com.werp.suporte.dbobj.table.Grupos)
        res_Grupos.elementAt(0);
      f.setGrupos_id(r.getGrupos_id() + "");
      f.setGrupos_nome((r.getGrupos_nome() != null) ?
          r.getGrupos_nome() + "":"");
      f.setPos("0");
      session.setAttribute("pos0","0");
    } else {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.norowsselected",""));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
    }
    session.setAttribute("DetalheGruposActionForm",f);
  }

}
